package ch.unisi.inf.java.project.riskbami.view;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import ch.unisi.inf.java.project.riskbami.model.*;
/**
 * Class to handle the Slider.
 * It helps the user to move armies from one country to another.
 * 
 * @author babazadm@lu.unisi.ch
 */
public final class Slider{
 
     private final JLabel jLabel;
     private final JFrame fortifyFrame;
     private final int min;
     private final int max;
     private final JSlider slider;
     private final RiskFrame gameFrame;
     private final Country src, dest;
     private final int type;
     
     private static final int SLIDE_FORTIFY = 5;
     private static final int SLIDE_ATTACK = 6;
     
     /**
      * Constructor for the class Slider
      */
     public Slider(final int min, final int max, final RiskFrame frame, final Country src, final Country dest, final int typeOfSlider)
     {
     fortifyFrame = new JFrame("Set the number of armies to move");
     gameFrame = frame;
     this.min = min;
     this.max = max;
     this.src = src;
     this.dest = dest;
     this.type = typeOfSlider;
     
     //Container settings
     final Container contentPane = fortifyFrame.getContentPane();
     contentPane.setLayout(new BorderLayout());
     
     //Slider settings
     slider = new JSlider(min, max);
     slider.setMajorTickSpacing(5);
     slider.setMinorTickSpacing(1);
     slider.setPaintLabels(true);
     slider.setPaintTicks(true);
     slider.addChangeListener(new MyChangeAction());
     jLabel = new JLabel();
     final Font font = jLabel.getFont();
     final Font font1 = font.deriveFont(19.0f);
     jLabel.setFont(font1);
     
     //Set the slider panel
     final JPanel sliderPanel = new JPanel();
     sliderPanel.setMinimumSize(new Dimension(300, 50));
     final JPanel numberPanel = new JPanel();
     final JPanel buttonPanel = new JPanel();
     
     //Set the OK button
     final JButton button = new JButton("Ok");
     button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {sendValue();}
         });
     buttonPanel.add(button);
     sliderPanel.add(slider);
     numberPanel.add(jLabel);
     numberPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Number Of Armies"));
     contentPane.add(slider, BorderLayout.NORTH);
     
     //Add everything to the content pane
     contentPane.add(numberPanel, BorderLayout.CENTER);
     contentPane.add(buttonPanel, BorderLayout.SOUTH);
     
     //Set the frame stuff
     fortifyFrame.setSize(500, 190);
     fortifyFrame.setVisible(true);
    }
    
    
    /**
     *Once the value is sended we just need to set the frame off 
     */
     private final void sendValue(){
         fortifyFrame.setVisible(false);
         int howMany = this.getNumberOfArmies();
         if(type == SLIDE_FORTIFY){
             FortifyCommand fortify = new FortifyCommand(getGameFrame().getGame(), src, dest, howMany);
                if(fortify.canExecute()){
                    fortify.execute();
                    if(src != null && dest != null){
                        gameFrame.appendCombatLog("Fortified from "+src+" to "+dest+" with "+howMany+" armies \n");
                    }
                }
            } else if(type == SLIDE_ATTACK){
                gameFrame.getGame().getCurrentPlayer().fortify(src, dest, howMany);
            }
     }

     /**
      * Returns the slider
      */
     protected final JSlider getSlider(){
         return slider;
      }
      
      /**
       * Returns the game frame
       */
      protected final RiskFrame getGameFrame(){
          return gameFrame;
      }
 

      /**
       * Returns the frame of the Slider
       */
      protected final JFrame getFrame(){
          return fortifyFrame;
      }
     
      /**
       * Returns the number of armies
       */
      private final int getNumberOfArmies(){
          return getSlider().getValue();
      }
      
      /**
       * Inner class to handle the ChangeEvent.
       */
      private final class MyChangeAction implements ChangeListener{
          
          public final void stateChanged(final ChangeEvent ce){
              int value = slider.getValue();
              String str = Integer.toString(value);
              jLabel.setText(str);
          }
      }
}